package com.ruoyi.thread.controller;


import com.ruoyi.thread.entity.UserDashboard;
import com.ruoyi.thread.service.ThreadO2Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;

@RestController
@Slf4j
@RequestMapping("/thread02")
public class ThreadO2Controller {
    @Autowired
    ThreadO2Service threadO2Service;
    @RequestMapping("/userDashBoard")
    public UserDashboard getUserDashboard(Long userId){
        long startTime = System.currentTimeMillis();

        // 单个任务可以用此方法统计耗时
        CompletableFuture<UserDashboard> userDashboard = threadO2Service.getUserDashboard(userId).whenComplete(
                (userDashboard2, throwable) -> {
                    log.info("任务执行完毕");
                    long endTime = System.currentTimeMillis();
                    log.info("当前任务耗时为ms：{}",endTime-startTime);
                }
        );
//        UserDashboard userDashboard1 = userDashboard.join();

        // 多个任务统计耗时可以用CompleteFuture.allof().join()
        CompletableFuture.allOf(userDashboard).join();
        Long endTime2 = System.currentTimeMillis();
        log.info("使用多个任务统计耗时为ms：{}",endTime2-startTime);
        return userDashboard.join();
    }
}
